package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.constants.AppConsrants;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.LoadDialogUtils;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class CircleActivity extends Activity
{

    private TextView textBookName;
    private TextView textAuthor;
    private TextView textSubTime;
    private TextView textISBN;
    private TextView textExpireTime;
    private TextView textStatus;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle);


        Intent intent = getIntent();
        String borrowId = intent.getStringExtra(AppConsrants.BORROW_ID);

        if ("".equalsIgnoreCase(borrowId))
        {
            ToastUtils.makeText(CircleActivity.this, "内部错误，请稍候重试");
            finish();
        }

        textBookName = (TextView) findViewById(R.id.textBookName);
        textAuthor = (TextView) findViewById(R.id.textAuthor);
        textSubTime = (TextView) findViewById(R.id.textSubTime);
        textISBN = (TextView) findViewById(R.id.textISBN);
        textExpireTime = (TextView) findViewById(R.id.textExpireTime);
        textStatus = (TextView) findViewById(R.id.textStatus);


        LoadDialogUtils.show(CircleActivity.this,"","加载中...");

        initBorrow(Integer.parseInt(borrowId));


    }

    private void initBorrow(int borrId)
    {
        AsyncHttpClient client = new AsyncHttpClient();

        RequestParams params = new RequestParams();
        params.add("borrId", borrId + "");

        client.post(ApiAddress.getApiDomain() + "/client/ClientServlet?op=getBorrowById", params, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);

LoadDialogUtils.hidden();
                try
                {

                    JSONObject jsonObj = new JSONObject(body);

                    int id = jsonObj.getInt("id");
                    int bookId = jsonObj.getInt("bookId");
                    int userId = jsonObj.getInt("userId");
                    int isDeory = jsonObj.getInt("isDeory");
                    int isReturn = jsonObj.getInt("isReturn");
                    String expireTime = jsonObj.getString("expireTime");
                    String subTime = jsonObj.getString("subTime");
                    String imageSrc = jsonObj.getString("imageSrc");
                    String bookName = jsonObj.getString("bookName");
                    String isbn = jsonObj.getString("isbn");
                    String author = jsonObj.getString("author");

                    textBookName.setText(bookName);
                    textAuthor.setText(author);
                    textSubTime.setText(subTime);
                    textExpireTime.setText(expireTime);
                    textISBN.setText(isbn);
                    textStatus.setText(isReturn==0?"借阅成功(未归还)":"借阅成功(已经归还)");

                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                LoadDialogUtils.hidden();
                ToastUtils.makeText(getApplication(), "网络连接错误");
            }
        });
    }

    public void back(View v)
    {
        finish();
    }


}
